<?php

/**
* 用来在文章撰写和编辑页面添加产品的新属性
*/

function product_info(){
	//空函数，以后再来考虑做什么
}

function product_add_custom_box() {
    add_meta_box( 
        'product_info',
        '商品信息',
        'product_info_box',
        'post' 
    );
}

function product_info_box(){
	global $prefix,$post;
	wp_nonce_field(plugin_basename(__FILE__),'product_info');
	@$product_tcode = get_post_meta($post->ID,'_product_tcode',true);
	@$product_zcode = get_post_meta($post->ID,'_product_zcode',true);
	@$product_brand = get_post_meta($post->ID,'_product_brand',true);
	@$product_price = get_post_meta($post->ID,'_product_price',true);
	@$product_cprice = get_post_meta($post->ID,'_product_cprice',true);
	@$product_storage = get_post_meta($post->ID,'_product_storage',true);
	@$product_size = get_post_meta($post->ID,'_product_size',true);
	@$product_color = get_post_meta($post->ID,'_product_color',true);
	@$brands = stripslashes(get_option($prefix.'brand'));
	@$brands = explode("\n",$brands);
	@$post_custom_type = get_post_meta($post->ID,'_post_custom_type',true);
?>
<p>这篇文章是否发布为 产品/普通文章 ？</p>
<select name="post_custom_type" id="post_custom_type_selectid">
	<option<?php if($post_custom_type == 'product')echo ' selected="selected"'; ?> value="product">要卖产品</option>
	<option<?php if($post_custom_type == 'post')echo ' selected="selected"'; ?> value="post">普通文章</option>
</select>
<div id="product-info-box">
<p>唯一条码：
<input name="product_tcode" type="text" size="16" class="form-input-tip" value="<?php echo $product_tcode; ?>" /></p>
<p>自编码：
<input name="product_zcode" type="text" size="16" class="form-input-tip" value="<?php echo $product_zcode; ?>" /></p>
<p>品牌：<select name="product_brand"><?php
	foreach($brands as $brand){
		echo '<option';
		if($brand == $product_brand)echo ' selected="selected"';
		echo '>';
		echo $brand;
		echo '</option>';
	}
?></select></p>
<p>零售价：
<input name="product_price" type="text" size="16" class="form-input-tip" value="<?php echo $product_price; ?>" />
(不用填￥)</p>
<p>促销价：
<input name="product_cprice" type="text" size="16" class="form-input-tip" value="<?php echo $product_cprice; ?>" />
(不用填￥，如果没有促销价，不填)</p>
<p>库存：
<input name="product_storage" type="text" size="16" class="form-input-tip" value="<?php echo $product_storage; ?>" /></p>
<p>尺寸：
<input name="product_size" type="text" size="16" class="form-input-tip" value="<?php echo $product_size; ?>" /></p>
<p>颜色：
<input name="product_color" type="text" size="16" class="form-input-tip" value="<?php echo $product_color; ?>" /></p>
</div>
<script src="<?php bloginfo('template_url'); ?>/js/jquery-1.7.1.min.js"></script>
<script src="<?php bloginfo('template_url'); ?>/js/javascript.functions.js"></script>
<script src="<?php bloginfo('template_url'); ?>/theme/javascript.js"></script>
<?php
}

function save_post_custom_data($post_id) {
  if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
	if(wp_verify_nonce(@$_POST['product_info'],plugin_basename(__FILE__))){
		$post_custom_type = @$_POST['post_custom_type'];
		if($post_custom_type == '')delete_post_meta($post_id,'_post_custom_type');else
		update_post_meta($post_id,'_post_custom_type',$post_custom_type)
		or add_post_meta($post_id,'_post_custom_type',$post_custom_type,true);
		$product_tcode = @$_POST['product_tcode'];
		if($product_tcode == '')delete_post_meta($post_id,'_product_tcode');else
		update_post_meta($post_id,'_product_tcode',$product_tcode)
		or add_post_meta($post_id,'_product_tcode',$product_tcode,true);
		$product_zcode = @$_POST['product_zcode'];
		if($product_zcode == '')delete_post_meta($post_id,'_product_zcode');else
		update_post_meta($post_id,'_product_zcode',$product_zcode)
		or add_post_meta($post_id,'_product_zcode',$product_zcode,true);
		$product_brand = @$_POST['product_brand'];
		if($product_brand == '')delete_post_meta($post_id,'_product_brand');else
		update_post_meta($post_id,'_product_brand',$product_brand)
		or add_post_meta($post_id,'_product_brand',$product_brand,true);
		$product_price = @$_POST['product_price'];
		if($product_price == '')delete_post_meta($post_id,'_product_price');else
		update_post_meta($post_id,'_product_price',$product_price)
		or add_post_meta($post_id,'_product_price',$product_price,true);
		$product_cprice = @$_POST['product_cprice'];
		if($product_cprice == '')delete_post_meta($post_id,'_product_cprice');else
		update_post_meta($post_id,'_product_cprice',$product_cprice)
		or add_post_meta($post_id,'_product_cprice',$product_cprice,true);
		$product_storage = @$_POST['product_storage'];
		if($product_storage == '')delete_post_meta($post_id,'_product_storage');else
		update_post_meta($post_id,'_product_storage',$product_storage)
		or add_post_meta($post_id,'_product_storage',$product_storage,true);
		$product_size = @$_POST['product_size'];
		if($product_size == '')delete_post_meta($post_id,'_product_size');else
		update_post_meta($post_id,'_product_size',$product_size)
		or add_post_meta($post_id,'_product_size',$product_size,true);
		$product_color = @$_POST['product_color'];
		if($product_color == '')delete_post_meta($post_id,'_product_color');else
		update_post_meta($post_id,'_product_color',$product_color)
		or add_post_meta($post_id,'_product_color',$product_color,true);
	}else return;
}

add_action( 'add_meta_boxes', 'product_add_custom_box',0,0 );
add_action( 'save_post', 'save_post_custom_data' );

?>